import axios, { AxiosResponse } from "axios";
import { OrderModel } from "../models/Order";
import { UserModel } from "../models/User";

class OrderApis {
    submitOrder(order: OrderModel): Promise<OrderModel> {
        let promise: Promise<OrderModel> = new Promise((resolve, reject) => {
            axios.post('/api/order/order', order)
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        let order: OrderModel = result.data.data;
                        resolve(order);
                    } else {
                        reject(result.data);
                    }
                })
                .then((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryUnfinishedOrder(id: string): Promise<Array<OrderModel>> {
        let promise: Promise<Array<OrderModel>> = new Promise((resolve, reject) => {
            let user: UserModel = {
                id: id
            };
            axios.get('/api/order/order', { params: user })
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        let orderList: Array<OrderModel> = result.data.data;
                        resolve(orderList);
                    } else {
                        reject(result.data);
                    }
                })
                .then((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryFinishedOrder(id: string): Promise<Array<OrderModel>> {
        let promise: Promise<Array<OrderModel>> = new Promise((resolve, reject) => {
            let user: UserModel = {
                id: id
            };
            axios.get('/api/order/finished', { params: user })
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        let orderList: Array<OrderModel> = result.data.data;
                        resolve(orderList);
                    } else {
                        reject(result.data);
                    }
                })
                .then((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    updateOrder(id: number): Promise<OrderModel> {
        let promise: Promise<OrderModel> = new Promise((resolve, reject) => {
            let obj: object = {
                id: id
            };
            axios.put('/api/order/order', obj)
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        let order: OrderModel = result.data.data;
                        resolve(order);
                    } else {
                        resolve(result.data);
                    }
                })
                .then((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const orderApis: OrderApis = new OrderApis();